package com.doge.service.admin.searchhistory;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.searchhistory.SearchHistory;
import com.doge.bean.pojo.searchhistory.SearchHistoryExample;
import com.doge.mapper.SearchHistoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.CredentialException;
import java.util.List;

/**
 * @program: project2-wechat-mall
 * @description: 历史搜索
 * @author: zhu lei
 * @create: 2021-11-30 16:25
 **/
@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Override
    public BaseData historyList(Integer userId, String keyword, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        searchHistoryExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        return BaseData.list(searchHistories,total);
    }
}